package com.shcz.migu.utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

@SuppressLint("SimpleDateFormat")
public class DateUtil {
	public static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
	public static final String ISO8601DATE_FORMAT_F = "yyyy-MM-dd'T'HH:mm:ss";
	public static final String ISO8601DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
	public static final String ISO8601DATE_WITH_MILLS_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
	public static final String ISO8601DATE_WITH_ZONE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
	public static final String ISO8601DATE_WITH_ZONE_MILLS_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
	public static final String ISO8601DATE_WITH_ZONE_MILLS_X_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm";
	public static final String DATE_FORMAT_3 = "yyyy.MM.dd";
	public static final String DATE_FORMAT_4 = "yyyy.MM.dd HH:mm:ss";
	public static final String DATE_FORMAT_5 = "MM-dd HH:mm";
	public static final String DATE_FORMAT_6 = "HH:mm";
	public static final String DATE_FORMAT_7 = "yyyyMMdd";
	public static final String DATE_FORMAT_8 = "MM-dd";
	public static final String DATE_FORMAT_9 = "yyyy/M/d";
	public static final String DATE_FORMAT_10 = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT_11 = "yyyy年MM月dd日";
	public static final String DATE_FORMAT_12 = "MM月dd日";
	public static final String DATE_FORMAT_13 = "EEEE HH:mm";
	public static final String DATE_FORMAT_14 = "yyyy年MM月";
	public static final String DATE_FORMAT_15 = "HH:mm:ss";
	public static final String DATE_FORMAT_16 = "yyyy-MM-dd HH:mm";

	public static final int ISO8601DATE_FORMAT_VALUE_LENGTH = ISO8601DATE_FORMAT.length() - 4;

	public static Date parseISO8601Date(String s) throws Exception {
		if (s == null || s.isEmpty()) {
			return null;
		}
		Date date = null;
		if (s.charAt(s.length() - 1) == 'Z') {
			String format = (s.length() == ISO8601DATE_FORMAT_VALUE_LENGTH) ? ISO8601DATE_FORMAT : ISO8601DATE_WITH_MILLS_FORMAT;
			DateFormat dateFormat = new SimpleDateFormat(format);
			dateFormat.setTimeZone(GMT_TIMEZONE);
			date = dateFormat.parse(s);
		}else if (s.charAt(s.length() - 1) == 'X') {
			DateFormat dateFormat = new SimpleDateFormat(ISO8601DATE_WITH_ZONE_MILLS_X_FORMAT);
			date = dateFormat.parse(s);
		}else if (s.length() == DATE_FORMAT.length()) {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
			dateFormat.setTimeZone(GMT_TIMEZONE);
			date = dateFormat.parse(s);
		} else if (s.indexOf('.') > 0) {
			date = new SimpleDateFormat(ISO8601DATE_WITH_ZONE_MILLS_FORMAT).parse(s);
		} else if (s.length() == 8) {
			date = new SimpleDateFormat(DATE_FORMAT_7).parse(s);
		} else {
			date = new SimpleDateFormat(ISO8601DATE_WITH_ZONE_FORMAT).parse(s);
		}
		return date;
	}

	public static String formateGrenLocalData(String timeDate, String formateStr, String newFormateStr) {

		Calendar calendar = Calendar.getInstance();
		TimeZone desTz = TimeZone.getTimeZone(calendar.getTimeZone().getID());
		Date mydate = null;
		Long milis = Long.valueOf(0);
		SimpleDateFormat df = new SimpleDateFormat(formateStr);
		SimpleDateFormat newFormate = new SimpleDateFormat(newFormateStr);
		try {
			mydate = df.parse(timeDate);
			milis = mydate.getTime() + desTz.getRawOffset();
			timeDate = newFormate.format(new Date(milis));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timeDate;
	}
	public static String formatISO8601Date(Date date) {
		DateFormat dateFormat = new SimpleDateFormat(ISO8601DATE_FORMAT);
		dateFormat.setTimeZone(GMT_TIMEZONE);
		return dateFormat.format(date);
	}

	public static String formatISO8601DateWithMills(Date date) {
		DateFormat dateFormat = new SimpleDateFormat(ISO8601DATE_WITH_MILLS_FORMAT);
		dateFormat.setTimeZone(GMT_TIMEZONE);
		return dateFormat.format(date);
	}

	public static String formatDate(Date date) {
		// DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT,
		// Locale.getDefault());
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
		dateFormat.setTimeZone(GMT_TIMEZONE);
		return dateFormat.format(date);
	}

	/**
	 * 格式：yyyy-MM-dd
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDateFromString(String dateString) {
		try {
			return formatDate(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：yyyy-MM-dd HH:mm
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate2FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_2);
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：yyyy.MM.dd
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate3FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_3);
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：yyyy.MM.dd HH:mm:ss
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate4FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_4);
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：MM-dd HH:mm
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate5FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_5);
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：HH:mm
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate6FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_6);
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：HH:mm
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate7FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_7);
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：MM-dd
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate8FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat("MM-dd");
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}

	/**
	 * 格式：yyyy/M/d
	 * 
	 * @param dateString
	 * @return
	 */
	public static String getFormatDate9FromString(String dateString) {
		try {
			DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_9);
			return dateFormat.format(parseISO8601Date(dateString));
		} catch (Exception e) {
			return dateString;
		}
	}
	
	
	/**
	 * getOffsetDays
	 * 
	 * @param dateString
	 * @return days
	 */
	public static int getOffsetDaysToTodayFromString(String dateString) {
		try {

			Date date = parseISO8601Date(dateString);
			Date curDate = new Date();
			String currentdateStr = date2Str(curDate, DateUtil.DATE_FORMAT);
			Date str2Date = str2Date(currentdateStr, DateUtil.DATE_FORMAT);
			String dateStr = date2Str(date, DateUtil.DATE_FORMAT);

			if (currentdateStr.equals(dateStr)) {
				return 0;
			}

			if (date.getTime() > str2Date.getTime()) {
				return (int) ((date.getTime() - str2Date.getTime()) / (1000 * 3600 * 24));
			} else {
				return (int) ((date.getTime() - str2Date.getTime()) / (1000 * 3600 * 24) - 1);
			}

		} catch (Exception e) {
		}

		return 0;
	}
	
	/**
	 * Date To String
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Str(Date date, String format) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	/**
	 * String To Date
	 * @param str
	 * @param format
	 * @return Date
	 */
	public static Date str2Date(String str, String format) {
		if (null == str) {
			return null;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String convertMilToMinit(int mil){
//        Date date=new Date(mil);
//        SimpleDateFormat sdf = new SimpleDateFormat(format);
//        return sdf.format(date);
//		int minit=00,mils=00;
//		String result;
//		if(mil/60>0)
//		{
//			minit=mil/60;
//		}
//		mils=mil%60;
//		if(mils<10)
//		{
//			result=""+minit+":0"+mils;
//		}else result=""+minit+":"+mils;
//		return result ;
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm:ss");
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
		return simpleDateFormat.format(mil);
//		Log.i(TAG,"--shijian=="+simpleDateFormat.format(mil));
	}
	
	/**
	 * String To Calendar
	 * @param dateTime
	 * @param format
	 * @return Calendar
	 */
	public static Calendar convertToCanlendar(String dateTime, String format) {
		Calendar calendar = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date date = sdf.parse(dateTime);
			calendar = Calendar.getInstance();
			calendar.setTime(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendar;
	}
	/**
	 * 转换日期格式
	 * @param dateString
	 * @param oldFormatStr
	 * @param newFormatStr
	 * @return
	 */
	public static String getConvertDateString(String dateString, String oldFormatStr, String newFormatStr) {
		String dateTime = "";
		try {
			DateFormat format = new SimpleDateFormat(oldFormatStr);
			Date date = format.parse(dateString);
			SimpleDateFormat sdf = new SimpleDateFormat(newFormatStr);
			dateTime = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateTime;
	}
	
	/**
	 * long2str
	 * @param longdate
	 * @param format
	 * @return
	 */
	public static String long2str(long longdate, String format) {
		Date date = new Date(longdate);
		return date2Str(date, format);
	}
	
	
	/**
	 * 日期的毫秒值   返回时间戳
	 * @param str   时间
	 * @param format		时间格式
	 * @return
	 */
	public static long getMillisecondByDateTime(String str, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		long millionSeconds = 0;
		try {
			millionSeconds = sdf.parse(str).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return millionSeconds;
	}

    /**
     * 日期的毫秒值
     *
     * @param dateString
     * @return
     */
    public static long getMillisecondByDateString(String dateString){
        try {
            return parseISO8601Date(dateString).getTime();
        } catch (Exception e) {
            return System.currentTimeMillis();
        }
    }
	
	/**
	 * 两个日期相差的天数
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int daysOfTwo(Date startDate, Date endDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		int sday = calendar.get(Calendar.DAY_OF_YEAR);

		calendar.setTime(endDate);
		int eday = calendar.get(Calendar.DAY_OF_YEAR);

		return eday - sday;
	}
	
	public static int compareDate(Date date1, Date date2) {
		try {
            if (date1.getTime() > date2.getTime()) {
                return 1;
            } else if (date1.getTime() < date2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

	public static boolean compareDateLong(Long date1, Long date2) {
		try {
			if (date1 > date2) {
				return true;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return false;
	}


	public static String seconds2Str(long second) {
		long milliseconds = second * 1000;
//		milliseconds += getMillisecondByDateTime(LocalConstants.BASE_TIMELINE, DATE_FORMAT_10);
		milliseconds += 1262275200000L;
		return long2str(milliseconds, DATE_FORMAT_15);
	}

	public static long seconds2long(long second) {
		long milliseconds = second * 1000;
//		milliseconds += getMillisecondByDateTime(LocalConstants.BASE_TIMELINE, DATE_FORMAT_10);
		milliseconds += 1262275200000L;
		return milliseconds;
	}
	public static String getBeforeDay(String dateStr, String oldformat, String newFormate){
		String date= DateUtil.getConvertDateString(dateStr,oldformat
				,newFormate);
		return getCompareIntDay(date, getCurrentTime(DateUtil.DATE_FORMAT_10),DateUtil.DATE_FORMAT_10);
	}
	public static String getCurrentTime(String timeFormate) {
		long newestRequestTime = System.currentTimeMillis();//"yyyy-MM-dd HH:mm:ss"
		String time = getDateStr(newestRequestTime, timeFormate);
		return time;
	}
	public static String getDateStr(long time, String format) {
		if (time == 0)
			return null;
		Date date = new Date(time);
		return getDateStr(date, format);
	}
	public static String getDateStr(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		String dateStr = dateFormat.format(date);
		return dateStr;
	}

	public static String getCompareIntDay(String dateStr, String newDateStr, String formate) {
		SimpleDateFormat formatter = new SimpleDateFormat(formate);
		Date oldDate=null;
		Date newDate=null;
		try {
			oldDate=formatter.parse(dateStr);
			newDate =formatter.parse(newDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		newDate.compareTo(oldDate);
		long newLong=newDate.getTime();
		long oldLong=oldDate.getTime();
		int hour= (int) ((newLong-oldLong)/1000/60/60);
		if(hour/24>0)
		{
			return hour/24+"天前";
		}else
			return hour+"小时前";


	}
	/**
	 * 根据当前日期获得是星期几
	 *
	 * @return
	 */
	public static String getWeek(String time) {
		String Week = "周";


		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();
		try {


			c.setTime(format.parse(time));


		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			Week += "日";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 2) {
			Week += "一";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 3) {
			Week += "二";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 4) {
			Week += "三";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 5) {
			Week += "四";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 6) {
			Week += "五";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 7) {
			Week += "六";
		}
		return Week;
	}

}
